/**
 * @author casazaft
 */
Package("xclab.utils").Class("AbstractLogger",{
	AbstractLogger:function(level){
	        level = level || this.getDefaultLevel();
        this.level = level;
    },
    Prototype: {
        getDefaultLevel: function(){
            return this.getClass().DEBUG;
        },
        setLevel: function(level){
            this.level = level;
        },
        getLevel: function(){
            return this.level;
        },
        debug: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.DEBUG) {
                this._log(s, "DEBUG");
            }
        },
        info: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.INFO) {
                this._log(s, "INFO");
            }
        },
        warn: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.WARN) {
                this._log(s, "WARN");
            }
        },
        error: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.ERROR) {
                this._log(s, "ERROR");
            }
        },
        fatal: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.FATAL) {
                this._log(s, "FATAL");
            }
        },
        _log: function(msg, level, color){
        }
    },
    Statics: {
        DEBUG: 1,
        INFO: 2,
        WARN: 3,
        ERROR: 4,
        FATAL: 5,
        NONE: 6
    }
});
Package("xclab.utils").Class("PopupLogger",{
	PopupLogger:function(level, id){
	        this.Super.constructor.apply(this, arguments);
        this.init(id);
    },
    Statics: {
        DEBUG_COLOR: "green",
        INFO_COLOR: "blue",
        WARN_COLOR: "yellow",
        ERROR_COLOR: "red",
        FATAL_COLOR: "red"
    },
    Prototype: {
        init: function(id){
            if (!this._window || !this._window.document) {
                this._window = window.open("", id, 'width="600",height=400,scrollbars=1,status=0,toolbars=0,resizable=1');
                if (!this._window) {
                    obj.popupBlocker = true;
                    alert("You have a popup window manager blocking the log4js log popup display.\n\nThis must be disabled to properly see logged events.");
                    return;
                }
                this._window.resizeTo(screen.width, screen.height);
                this._window.moveTo(0, 0);
                if (!this._window.document.getElementById('loggerTable')) {
                    this._window.document.writeln("<table width='100%' border='1' id='loggerTable'><tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr></table>");
                    this._window.document.close();
                }
            }
            this.tbl = this._window.document.getElementById("loggerTable");
        },
        getType: function(obj){
            if (obj instanceof Array) 
                return "Array";
            else 
                if (obj instanceof Date) 
                    return "Date";
                else 
                    if (obj instanceof RegExp) 
                        return "Regex";
                    else 
                        if (obj instanceof Function) 
                            return "Function";
                        else 
                            return typeof obj;
        },
        travelObj: function(obj){
            if (typeof obj != "object") {
                return obj;
            }
            var tmp = [];
            if (obj instanceof Array) {
                tmp.push("<table border='1' style='border-color:black;border-collapse:collapse;'><tr><th>index</th><th>type</th><th>value</th></tr>");
                for (var i = 0; i < obj.length; i++) {
                    var value = obj[i];
                    var type = this.getType(value);
                    if (value == null) 
                        value = "null";
                    tmp.push(System.out.format("<tr style='border-color:black;'><td>{0}</td><td>{1}</td><td>{2}</td></tr>", i, type, value));
                }
            }
            else {
                tmp.push("<table border='1' style='border-color:black;border-collapse:collapse;'><tr><th>name</th><th>type</th><th>value</th></tr>");
                for (var p in obj) {
                    try {
                        var value = obj[p];
                        var type = this.getType(value);
                        if (value == null) 
                            value = "null";
                        tmp.push(System.out.format("<tr style='border-color:black;'><td>{0}</td><td>{1}</td><td>{2}</td></tr>", p, type, value));
                    } 
                    catch (e) {
                    }
                }
            }
            tmp.push("</table>");
            tmp = tmp.join("");
            return tmp;
        },
        _log: function(msg, level, color){
            if (!color) {
                var thisClass = this.getClass();
                color = thisClass[level + "_COLOR"];
            }
            var tbl = this._window.document.getElementById("loggerTable");
            if (!tbl) 
                this.init();
            var row = tbl.insertRow(-1);
            var cell_1 = row.insertCell(-1);
            var cell_2 = row.insertCell(-1);
            var cell_3 = row.insertCell(-1);
            var d = new Date();
            var h = d.getHours();
            if (h < 10) {
                h = "0" + h;
            }
            var m = d.getMinutes();
            if (m < 10) {
                m = "0" + m;
            }
            var s = d.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }
            var date = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear() + "&nbsp;-&nbsp;" + h + ":" + m + ":" + s;
            cell_1.style.fontSize = "10pt";
            cell_1.style.fontWeight = "bold";
            cell_1.style.paddingRight = "6px";
            cell_2.style.fontSize = "10pt";
            cell_2.style.color = color;
            cell_3.style.fontSize = "10pt";
            cell_3.style.whiteSpace = "nowrap";
            cell_3.style.width = "100%";
            if (tbl.rows.length % 2 === 0) {
                cell_1.style.backgroundColor = "#eeeeee";
                cell_2.style.backgroundColor = "#eeeeee";
                cell_3.style.backgroundColor = "#eeeeee";
            }
            cell_1.innerHTML = date;
            cell_2.innerHTML = level;
            cell_3.innerHTML = msg;
        },
        detail: function(msg, level, color){
            var result = this.travelObj(msg);
            this.info(result, level, color);
        },
        clear: function(){
            this._window.document.writeln("<table width='100%' id='loggerTable'><tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr></table>");
            this._window.document.close();
            this.tbl = this._window.document.getElementById("loggerTable");
        }
    },
    Extends: "xclab.utils.AbstractLogger"
});
window.log = new xclab.utils.PopupLogger(null, "log");

window.onerror = function(a, b, c){
    if (!System.err) 
        System.err = new xclab.utils.PopupLogger(null, "error");
    System.err.detail({
        msg: a,
        url: b,
        line: c
    });
    return true;
};
